package roomlogic

import (
	"common/dao/model"
	"context"
	"errors"
	"gorm.io/gorm"
	"time"

	"OemServer/internal/svc"
	"OemServer/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteLogic {
	return &DeleteLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DeleteLogic) Delete(in *pb.UuId) (*pb.Emp, error) {
	// todo: add your logic here and delete this line
	if in.Uuid == "" {
		return nil, errors.New("uuid is null")
	}
	db := l.svcCtx.DB
	table := db.Table(model.TableNameRoom)
	err := table.Transaction(func(tx *gorm.DB) error {
		var (
			room = model.Room{}
		)
		if err := tx.Where("uuid = ?", in.Uuid).First(&room).Error; err != nil {
			return err
		}
		if room.Status == 1 {
			return errors.New("房间正在使用")
		}
		room.IsDel = 1
		t := time.Now()
		room.DeleTime = &t
		if err := tx.Save(&room).Error; err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		return nil, err
	}
	return &pb.Emp{}, nil
}
